package com.xt.website_manager.dao;


import com.xt.website_manager.po.News;
import com.xt.website_manager.po.NewsExt;
import com.xt.website_manager.po.User;
import com.xt.website_manager.util.MyPageInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/*
 * @Author 爱尚实训赵国鑫
 * @ClassName  NewsDao
 * @Date 2019/8/18/13:38
 * @Description TODO:(一句话描述这个类)
 */
@Mapper
public interface NewsDao {

    /**
     *  根据一级类目查询全部新闻
     * @param cid
     */
    @Select("select tb_news.* from tb_news " +
            "inner join tb_categorysecond " +
            "on tb_news.csid=tb_categorysecond.id " +
            "inner join tb_category " +
            "on tb_category.id=tb_categorysecond.cid " +
            "where tb_category.id=#{cid}")
    @Results(id = "newsMap",value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "title", property = "title"),
            @Result(column = "content", property = "content"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "csid", property = "csid"),
            @Result(column = "uid", property = "uid")
    })
    List<News> findAllNewsByCid(Integer cid);

    /**
     * 复合查询新闻
     * @param
     * @return
     */
    @Select("<script>select tb_news.*,tb_user.username from tb_news <if test='csid != null'> inner join tb_categorysecond on tb_news.csid=tb_categorysecond.id </if> left join tb_user on tb_news.uid=tb_user.id <where><if test='csid != null'> and tb_news.csid = #{csid}</if><if test='title != null'> and title like '%${title}%'</if></where> order by tb_news.create_time desc</script>")
    @Results(id = "newsAndUserMap",value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "title", property = "title"),
            @Result(column = "content", property = "content"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "csid", property = "csid"),
            @Result(column = "uid", property = "uid"),
            @Result(column = "uid", property = "username",
                    one = @One(select = "com.xt.website_manager.dao.NewsDao.userDetails"))
    })
    List<NewsExt> findNewsList(NewsExt newsExt);

    /**
     *  查询上传新闻人
     */
    @Select("select username from tb_user where id = #{id}")
    String userDetails(Integer id);


    /**
     * 根据新闻id获取新闻详情
     * @param id   新闻id
     * @return
     */
    @Select("select id,title,content,create_time as createTime,csid,uid  from tb_news where id=#{id}")
    NewsExt findNewsByID(Integer id);

    /**
     * 根据新闻id获取上一条记录
     * @param id
     * @return
     */
    @Select("select id,title from tb_news where id = (select id from tb_news where id <#{id} order by create_time desc limit 1)")
    News finPrevNewsByID(Integer id);

    /**
     * 根据新闻id获取下一条记录
     * @param id
     * @return
     */
    @Select("select id,title from tb_news where id = (select id from tb_news where id >#{id} order by create_time asc limit 1)")
    News finNextNewsByID(Integer id);

    /**
     * 添加新闻
     */
    @Insert("insert into tb_news values(default,#{title},#{content},#{createTime},#{csid},#{uid})")
    void addNews(News news);

    /**
     * 删除新闻
     * @param id
     */
    @Delete("delete from tb_news where id=#{id}")
    void delNews(Integer id);


    /**
     * 修改新闻信息
     * @param newsExt
     */
    @Update("update tb_news set title=#{title},content=#{content},csid=#{csid} where id=#{id}")
    void updateNews(NewsExt newsExt);




    /**
     * 根据二级类目id获取新闻总数
     * @param csid
     * @return
     */
    @Select("select count(0) from tb_news where csid=#{csid}")
    Integer getNewsCount(Integer csid);

    /**
     *  根据uid查询新闻
     */
    @Select("select id from tb_news where uid=#{uid} limit 1")
    News findNewsByUid(Integer uid);

}
